using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SCSEngine.Services.Input;
using SCSEngine.Services;
using TriUan.TriUanGame.GameComponents.Polygon;
using SCSEngine.Mathematics;
using TriUan.TriUanGame.GameComponents.Polygon.Implements;
using SVLvsXNA;

namespace TriUan.TriUanGame.GameComponents
{
    public class Pattern : TUDrawableGameComponent
    {
        private SCSServices services;
        private SpriteBatch spriteBatch;
        private InputHandle inputHandle;

        public ITUPolygon Polygon;
        public ITUPolygon copyPolygon;
        public ITUPolygonInfo PolygonInfo;
        public Texture2D texture;

        SpriteEffects effect;

        private Texture2D point;

        public Vector2 Position
        {
            get { return PolygonInfo.Position; }
        }

        public Vector2 Origin
        {
            get { return Polygon.Origin; }
        }

        public float Scale
        {
            get { return PolygonInfo.Scale; }
            set { PolygonInfo.Scale = value;}
        }

        public Vector2 Center { get; set; }

        public Pattern(GamePage gamePage, ITUPolygon poligon, ITUPolygonInfo polygonInfo, Texture2D image)
            : base(gamePage)
        {
            services = (SCSServices)gamePage.Services.GetService(typeof(SCSServices));
            spriteBatch = services.SpriteBatch;
            inputHandle = services.InputHandle;
            this.effect = SpriteEffects.None;
            Polygon = poligon;
            copyPolygon = new TUPolygon(new List<Vector2>(Polygon.Vertices), Polygon.Origin);
            PolygonInfo = polygonInfo;
            Center = new Vector2(image.Width / 2 + Polygon.Origin.X, image.Height / 2 + Polygon.Origin.Y);
            this.texture = image;

            point = gamePage.Content.Load<Texture2D>("point");
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < Polygon.Vertices.Count; i++)
            {
                copyPolygon.Vertices[i] = Polygon.Vertices[i];
                copyPolygon.Vertices[i] *= 0.7f;
                copyPolygon.Vertices[i] += this.Position - Polygon.Origin;
                copyPolygon.Vertices[i] = GMath.Transform(copyPolygon.Vertices[i], this.Position, 0f);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(this.texture, this.Position, null, Color.White, 0, Center, this.Scale, effect, 1f);

            //spriteBatch.Draw(point, PolygonInfo.Position, Color.White);
            foreach (var item in copyPolygon.Vertices)
            {
                //spriteBatch.Draw(point, item, Color.White);
            }

            base.Draw(gameTime);
        }
    }
}
